package com.jeesite.modules.exam.quota.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeesite.common.config.Global;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.ServiceException;
import com.jeesite.common.utils.excel.ExcelImport;
import com.jeesite.common.validator.ValidatorUtils;
import com.jeesite.modules.exam.quota.entity.*;
import com.jeesite.modules.exam.socreResult.dao.DcSupExamOrgSelectDao;
import com.jeesite.modules.exam.socreResult.entity.DcSupExamOrgSelect;
import com.jeesite.modules.sys.utils.DictUtils;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.exam.quota.dao.DcSupExamQuotaInfoDao;
import com.jeesite.modules.file.utils.FileUploadUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import static com.jeesite.common.web.http.ServletUtils.renderResult;

/**
 * 考核任务指标表-指标信息Service
 * @author xhc
 * @version 2022-05-28
 */
@Service
@Transactional(readOnly=false)
public class DcSupExamQuotaInfoService extends CrudService<DcSupExamQuotaInfoDao, DcSupExamQuotaInfo> {
	@Autowired
	private DcSupExamQuotaOrgService dcSupExamQuotaOrgService;


	@Autowired
	private DcSupExamQuotaService dcSupExamQuotaService;
	@Autowired
	private DcSupExamOrgSelectDao dcSupExamOrgSelectDao;
	/**
	 * 获取单条数据
	 * @param dcSupExamQuotaInfo
	 * @return
	 */
	@Override
	public DcSupExamQuotaInfo get(DcSupExamQuotaInfo dcSupExamQuotaInfo) {

		return super.get(dcSupExamQuotaInfo);
	}
	
	/**
	 * 查询分页数据
	 * @param dcSupExamQuotaInfo 查询条件
	 * @param dcSupExamQuotaInfo.page 分页对象
	 * @return
	 */
	@Override
	public Page<DcSupExamQuotaInfo> findPage(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		return super.findPage(dcSupExamQuotaInfo);
	}
	
	/**
	 * 查询列表数据
	 * @param dcSupExamQuotaInfo
	 * @return
	 */
	@Override
	public List<DcSupExamQuotaInfo> findList(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		return super.findList(dcSupExamQuotaInfo);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param dcSupExamQuotaInfo
	 */
	@SneakyThrows
	@Override
	@Transactional(readOnly=false)
	public void save(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		if(StringUtils.isEmpty( dcSupExamQuotaInfo.getQuotaClass())){
			dcSupExamQuotaInfo.setQuotaClass("1");
		}
		DcSupExamQuotaInfo dcSupExamQuotaInfo_ = new DcSupExamQuotaInfo();
		dcSupExamQuotaInfo_.setQuotaId(dcSupExamQuotaInfo.getQuotaId());
		dcSupExamQuotaInfo_.setQuotaContent(dcSupExamQuotaInfo.getQuotaContent());
		List<DcSupExamQuotaInfo> list = dao.findList(dcSupExamQuotaInfo_);

		if(list!=null && list.size()> 0 &&StringUtils.isBlank(dcSupExamQuotaInfo.getId())){
			throw new ServiceException("目标内容重复");
		}
		super.save(dcSupExamQuotaInfo);
		if(dcSupExamQuotaInfo.getExameOrgs()!=null ) {
			String[] orgs = dcSupExamQuotaInfo.getExameOrgs().split(",");
			String[] orgNames = dcSupExamQuotaInfo.getExameOrgNames().split(",");
			DcSupExamQuotaOrg orgDelete = new DcSupExamQuotaOrg();
			orgDelete.setQuotaInfoId(dcSupExamQuotaInfo.getId());
			dcSupExamQuotaOrgService.deleteOrg(orgDelete);
			for (int i = 0; i < orgs.length; i++) {
				DcSupExamQuotaOrg dcSupExamQuotaOrg = new DcSupExamQuotaOrg();
				dcSupExamQuotaOrg.setQuotaId(dcSupExamQuotaInfo.getQuotaId());
				dcSupExamQuotaOrg.setExameOrg(orgs[i]);
				dcSupExamQuotaOrg.setExameOrgName(orgNames[i]);
				DcSupExamOrgSelect org = dcSupExamOrgSelectDao.getOrgByName(orgNames[i]);
				dcSupExamQuotaOrg.setOrgType(org.getOrgType());
				dcSupExamQuotaOrg.setQuotaInfoId(dcSupExamQuotaInfo.getId());
				dcSupExamQuotaOrg.setAssessMethod(dcSupExamQuotaInfo.getAssessMethod());
				dcSupExamQuotaOrg.setQuotaWeight(dcSupExamQuotaInfo.getQuotaWeight());
				dcSupExamQuotaOrg.setCurrentYaerGoal(dcSupExamQuotaInfo.getCurrentYaerGoal());
				dcSupExamQuotaOrg.setLastYaerGoal(dcSupExamQuotaInfo.getLastYaerGoal());
				dcSupExamQuotaOrg.setLastYearComplete(dcSupExamQuotaInfo.getLastYearComplete());
				dcSupExamQuotaOrg.setQuotaUnit(dcSupExamQuotaInfo.getQuotaUnit());
				dcSupExamQuotaOrg.setTimeString(System.currentTimeMillis()+"");
				dcSupExamQuotaOrg.setCreateDate(new Date());
				dcSupExamQuotaOrg.setRemarks(dcSupExamQuotaInfo.getRemarks());
				dcSupExamQuotaOrgService.save(dcSupExamQuotaOrg);
			}
			// 保存上传图片
			FileUploadUtils.saveFileUpload(dcSupExamQuotaInfo, dcSupExamQuotaInfo.getId(), "dcSupExamQuotaInfo_image");
			// 保存上传附件
			FileUploadUtils.saveFileUpload(dcSupExamQuotaInfo, dcSupExamQuotaInfo.getId(), "dcSupExamQuotaInfo_file");
			return;
		}
		FileUploadUtils.saveFileUpload(dcSupExamQuotaInfo.getId(), "dcSupExamQuotaInfo_file", dcSupExamQuotaInfo.getFileuploadId(), "");
	}
	
	/**
	 * 更新状态
	 * @param dcSupExamQuotaInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		super.updateStatus(dcSupExamQuotaInfo);
	}
	
	/**
	 * 删除数据
	 * @param dcSupExamQuotaInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		super.delete(dcSupExamQuotaInfo);
		dao.deleteQuotaOrg(dcSupExamQuotaInfo);
	}

	@Transactional(readOnly=false)
    public void saveStrList(List<DcSupExamQuotaInfo> dcSupExamQuotaInfos) {
		dcSupExamQuotaInfos.forEach(info->{
			super.save(info);
			FileUploadUtils.saveFileUpload(info.getId(), "dcSupExamQuotaInfo_file", info.getFileuploadId(), "");
		});
		dcSupExamQuotaService.updateFileStatusById(dcSupExamQuotaInfos.get(0).getQuotaId(),"2");
    }

	public Page<DcSupExamQuotaInfo> listKeyDataPage(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		Page<DcSupExamQuotaInfo> page = dcSupExamQuotaInfo.getPage();
		List <DcSupExamQuotaInfo> list = dao.listKeyData(dcSupExamQuotaInfo);
		page.setList(list);
		return page;
	}

    public Page<DcSupExamQuotaOrg> listDanOrgData(DcSupExamQuotaOrg dcSupExamQuotaOrg) {
		Page<DcSupExamQuotaOrg> page = dcSupExamQuotaOrg.getPage();
		List <DcSupExamQuotaOrg> list =  dao.listDanOrgData(dcSupExamQuotaOrg);
		page.setList(list);
		return page;
    }

	public List<DcSupExamQuotaInfoExportDan> exportDanList(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
			List <DcSupExamQuotaInfoExportDan> list = dao.listDanExportData(dcSupExamQuotaInfo);
			return list;
	}

	public List<DcSupExamQuotaInfoExportKey> exportKeyList(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		List <DcSupExamQuotaInfoExportKey> list = dao.listKeyExportData(dcSupExamQuotaInfo);
		return list;
	}

	@Transactional(readOnly=false)
	public String importDataDan(MultipartFile file,String quotaId,String examId,String examYear,String createOrg) {
		if (file == null){
			throw new ServiceException(text("请选择导入的数据文件！"));
		}
		int successNum = 0; int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		try(ExcelImport ei = new ExcelImport(file, 1, 0)){
			List<DcSupExamQuotaInfoExportDan> list = ei.getDataList(DcSupExamQuotaInfoExportDan.class);
			for (DcSupExamQuotaInfoExportDan info : list) {
				try{
					// 验证数据文件
					ValidatorUtils.validateWithException(info);
					// 任务年度为空验证
					if (StringUtils.isBlank(info.getQuotaType())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：类型不能为空");
						continue;
					}

					String quotaType = DictUtils.getDictValue("dc_quotaType_dan",info.getQuotaType(),"未知");
					if (StringUtils.equals("未知",quotaType)) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：系统中不存在此类型");
						continue;
					}
					info.setQuotaType(quotaType);
					if (StringUtils.isBlank(info.getQuotaContent())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：目标内容不能为空");
						continue;
					}

					if (StringUtils.isBlank(info.getExameOrgNames())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：考核单位不能为空");
						continue;
					}
					if (info.getSortNum()<1) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：序号不能小于1");
						continue;
					}
					if (info.getQuotaWeight()==null) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：权数不能为空");
						continue;
					}
					if (StringUtils.isBlank(info.getCurrentYaerGoal())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：本年度目标不能为空");
						continue;
					}
					if (StringUtils.isBlank(info.getAssessMethod())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：考核方式不能为空");
						continue;
					}
					String assess = DictUtils.getDictValue("dc_exam_assessment",info.getAssessMethod(),"未知");
					if (StringUtils.equals(assess,"未知")) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：系统没有此考核方式");
						continue;
					}
					String remarks = DictUtils.getDictValue("dc_exam_remarks",info.getRemarks(),"未知");
					if (StringUtils.equals(remarks,"未知")) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：系统没有此备注");
						continue;
					}
					info.setRemarks(remarks);
					info.setAssessMethod(assess);
					DcSupExamOrgSelect org = dcSupExamOrgSelectDao.getOrgByName(info.getExameOrgNames());
					if(org==null){
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：系统没有考核单位");
						continue;
					}else{
						if(StringUtils.equals(org.getOrgCode(),createOrg)){
							failureNum++;
							failureMsg.append("<br/>" + failureNum + " 导入失败：本单位不能考核本单位");
							continue;
						}
					}

					// 验证是否存在这个用户
					DcSupExamQuotaInfo dan = new DcSupExamQuotaInfo();
					BeanUtils.copyProperties(info,dan);
					dan.setExameOrgs(org.getOrgCode());
					dan.setOrgType(org.getOrgType());
					String quotaType1 = dan.getQuotaType();
					if(StringUtils.equals("11",quotaType1)){
						dan.setQuotaClass("1");
					}
					if(StringUtils.equals("13",quotaType1)){
						dan.setQuotaClass("3");
					}
					if(StringUtils.equals("14",quotaType1)){
						dan.setQuotaClass("2");
					}
					//校验 考核单位相同目标内容相同为重复
					dan.setQuotaId(quotaId);
					Integer count = dcSupExamQuotaOrgService.findGoalOrg(dan);
					if(count > 0) {
						failureNum++;
						failureMsg.append("<br/>第" + failureNum + " 行导入失败：数据重复");
						continue;
					}
					if("2".equals(dan.getQuotaClass())) {
						dan.setExameOrg(org.getOrgCode());
						dan.setExamId(examId);
						//查询同一报送同一机构不同指标的quotaWeight值
						Double quotaWeight = info.getQuotaWeight();
						List<DcSupExamQuotaInfo> dcSupExamQuotaInfoList = this.dao.findValidationOrgList(dan);
						if (!dcSupExamQuotaInfoList.isEmpty()) {
							for (DcSupExamQuotaInfo dseqi : dcSupExamQuotaInfoList) {
								quotaWeight += dseqi.getQuotaWeight();
							}
						}
						if (quotaWeight > 3) {
							failureNum++;
							failureMsg.append("<br/>" + failureNum + " 行导入失败：加分项指标：考核单位"+info.getExameOrgNames()+"总权数超过3");
							continue;
						}
					}

					DcSupExamQuotaInfo	danDb  = dao.findQuotaInfoExamId(dan.getQuotaContent(),examId,createOrg);
					if(danDb !=null){
						Long sortNum = danDb.getSortNum();
						Long sortNum1 = info.getSortNum();
						if(sortNum1!=sortNum){
							failureNum++;
							failureMsg.append("<br/>" + failureNum + " 导入失败：同一目标内容："+danDb.getQuotaContent()+"需要序号相同");
							continue;
						}
						dan.setId(danDb.getId());
					}
					dan.setExamYear( examYear);
					super.save(dan);
					DcSupExamQuotaOrg dcSupExamQuotaOrg = new DcSupExamQuotaOrg();
					dcSupExamQuotaOrg.setQuotaId(dan.getQuotaId());
					dcSupExamQuotaOrg.setExameOrg(dan.getExameOrgs());
					dcSupExamQuotaOrg.setExameOrgName(dan.getExameOrgNames());
					dcSupExamQuotaOrg.setQuotaInfoId(dan.getId());
					dcSupExamQuotaOrg.setAssessMethod(dan.getAssessMethod());
					dcSupExamQuotaOrg.setQuotaWeight(dan.getQuotaWeight());
					dcSupExamQuotaOrg.setCurrentYaerGoal(dan.getCurrentYaerGoal());
					dcSupExamQuotaOrg.setLastYaerGoal(dan.getLastYaerGoal());
					dcSupExamQuotaOrg.setLastYearComplete(dan.getLastYearComplete());
					dcSupExamQuotaOrg.setQuotaUnit(dan.getQuotaUnit());
					dcSupExamQuotaOrg.setCreateDate(new Date());
					dcSupExamQuotaOrg.setTimeString(new Date().getTime()+"");
					dcSupExamQuotaOrg.setOrgType(dan.getOrgType());
					dcSupExamQuotaOrg.setRemarks(dan.getRemarks());
					dcSupExamQuotaOrgService.save(dcSupExamQuotaOrg);

					successNum++;
					successMsg.append("<br/>" + successNum + "、单项指标导入成功");

				} catch (Exception e) {
					failureNum++;
					String msg = "<br/>" + failureNum + "、单项指标导入失败：";
					if (e instanceof ConstraintViolationException){
						ConstraintViolationException cve = (ConstraintViolationException)e;
						for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
							msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
						}
					}else{
						msg += e.getMessage();
					}
					failureMsg.append(msg);
					logger.error(msg, e);
				}
			}
		} catch (Exception e) {
			failureMsg.append(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		}else{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}
	@Transactional(readOnly=false)
	public String importDataKey(MultipartFile file, String quotaId,String exameOrgs,String exameOrgNames,String examYear) {
		if (file == null){
			throw new ServiceException(text("请选择导入的数据文件！"));
		}
		int successNum = 0; int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		try(ExcelImport ei = new ExcelImport(file, 1, 0)){
			List<DcSupExamQuotaInfoExportKeyUpdate> list = ei.getDataList(DcSupExamQuotaInfoExportKeyUpdate.class);
			for (DcSupExamQuotaInfoExportKeyUpdate info : list) {
				try{
					// 验证数据文件
					ValidatorUtils.validateWithException(info);
					// 任务年度为空验证
//					if (StringUtils.isBlank(info.getQuotaType())) {
//						failureNum++;
//						failureMsg.append("<br/>" + failureNum + " 导入失败：类型不能为空");
//						continue;
//					}
//					String quotaType = DictUtils.getDictValue("dc_quotaType",info.getQuotaType(),"未知");
//					if (!StringUtils.equals("部门重点工作",info.getQuotaType())) {
//						failureNum++;
//						failureMsg.append("<br/>" + failureNum + " 导入失败：类型请固定为部门重点工作");
//						continue;
//					}
//					info.setQuotaType("1");
					if (StringUtils.isBlank(info.getQuotaContent())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：目标内容不能为空");
						continue;
					}
					if (info.getSortNum()<1) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：序号不能小于1");
						continue;
					}
					if (info.getQuotaWeight()==null) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：权数不能为空");
						continue;
					}
					if (StringUtils.isBlank(info.getCurrentYaerGoal())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：本年度目标不能为空");
						continue;
					}
//					if (StringUtils.isBlank(info.getAssessMethod())) {
//						failureNum++;
//						failureMsg.append("<br/>" + failureNum + " 导入失败：考核方式不能为空");
//						continue;
//					}
//					String assess = DictUtils.getDictValue("dc_exam_assessment",info.getAssessMethod(),"");
//					if (StringUtils.equals(assess,"未知")) {
//						failureNum++;
//						failureMsg.append("<br/>" + failureNum + " 导入失败：系统没有此考核方式");
//						continue;
//					}
					if (StringUtils.isBlank(info.getRemarks())) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：备注不能为空");
						continue;
					}
					String remarks = DictUtils.getDictValue("dc_exam_remarks",info.getRemarks(),"未知");
					if (StringUtils.equals(remarks,"未知")) {
						failureNum++;
						failureMsg.append("<br/>" + failureNum + " 导入失败：系统没有此备注");
						continue;
					}
					info.setRemarks(remarks);
					DcSupExamOrgSelect org = dcSupExamOrgSelectDao.getOrgByName(exameOrgNames);
					// 验证是否存在这个用户
					DcSupExamQuotaInfo dan = new DcSupExamQuotaInfo();
					BeanUtils.copyProperties(info,dan);
					dan.setExamYear( examYear);
					dan.setAssessMethod("");
					dan.setQuotaType("1");
					dan.setExameOrgs(org.getOrgCode());
					dan.setOrgType(org.getOrgType());
					dan.setQuotaClass("1");
					dan.setExameOrgNames(exameOrgNames);
					//校验 考核单位相同目标内容相同为重复
					dan.setQuotaId(quotaId);
					Integer count = dcSupExamQuotaOrgService.findGoalOrg(dan);
					if(count > 0) {
						failureNum++;
						failureMsg.append("<br/>第" + failureNum + " 行导入失败：数据重复");
						continue;
					}

					super.save(dan);
					DcSupExamQuotaOrg dcSupExamQuotaOrg = new DcSupExamQuotaOrg();
					dcSupExamQuotaOrg.setQuotaId(dan.getQuotaId());
					dcSupExamQuotaOrg.setExameOrg(dan.getExameOrgs());
					dcSupExamQuotaOrg.setExameOrgName(dan.getExameOrgNames());
					dcSupExamQuotaOrg.setQuotaInfoId(dan.getId());
					dcSupExamQuotaOrg.setAssessMethod(dan.getAssessMethod());
					dcSupExamQuotaOrg.setQuotaWeight(dan.getQuotaWeight());
					dcSupExamQuotaOrg.setCurrentYaerGoal(dan.getCurrentYaerGoal());
					dcSupExamQuotaOrg.setLastYaerGoal(dan.getLastYaerGoal());
					dcSupExamQuotaOrg.setLastYearComplete(dan.getLastYearComplete());
					dcSupExamQuotaOrg.setQuotaUnit(dan.getQuotaUnit());
					dcSupExamQuotaOrg.setCreateDate(new Date());
					dcSupExamQuotaOrg.setTimeString(new Date().getTime()+"");
					dcSupExamQuotaOrg.setOrgType(dan.getOrgType());
					dcSupExamQuotaOrg.setRemarks(dan.getRemarks());
					dcSupExamQuotaOrgService.save(dcSupExamQuotaOrg);

					successNum++;
					successMsg.append("<br/>" + successNum + "、关键指标导入成功");

				} catch (Exception e) {
					failureNum++;
					String msg = "<br/>" + failureNum + "、关键指标导入失败：";
					if (e instanceof ConstraintViolationException){
						ConstraintViolationException cve = (ConstraintViolationException)e;
						for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
							msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
						}
					}else{
						msg += e.getMessage();
					}
					failureMsg.append(msg);
					logger.error(msg, e);
				}
			}
		} catch (Exception e) {
			failureMsg.append(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		}else{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}

	public Page<DcSupExamQuotaInfo> findDcSupExamQuotaInfoList(Page<DcSupExamQuotaInfo> page,DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		dcSupExamQuotaInfo.setPage(page);
		List<DcSupExamQuotaInfo> dcSupExamQuotaInfoList=this.dao.findDcSupExamQuotaInfoList(dcSupExamQuotaInfo);
		page.setList(dcSupExamQuotaInfoList);
		return page;
	}

	public String validationOrg(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		try {
			String[] orgs=dcSupExamQuotaInfo.getExameOrgs().split(",");
			for (String orgId:orgs) {
				Double quotaWeight=0d;
				/*
				 * 初始化quotaWeight 根据remarks字段值 判断是新增还是修改  新增页面remarks不传值 修改传1
				 * 新增包含当前页面的quotaWeight值  修改不包含;
				 */

				if(!"1".equals(dcSupExamQuotaInfo.getRemarks())){
					quotaWeight=dcSupExamQuotaInfo.getQuotaWeight();
				}
				dcSupExamQuotaInfo.setExameOrg(orgId);
				//查询同一报送同一机构不同指标的quotaWeight值
				List<DcSupExamQuotaInfo> dcSupExamQuotaInfoList=this.dao.findValidationOrgList(dcSupExamQuotaInfo);
				if(!dcSupExamQuotaInfoList.isEmpty()){
					for (DcSupExamQuotaInfo dseqi:dcSupExamQuotaInfoList) {
						quotaWeight+=dseqi.getQuotaWeight();
					}
				}
				if(quotaWeight>3){
					return renderResult(Global.FALSE, "加分项指标单个机构总权数不能超过3");
				}
			}
			return renderResult(Global.TRUE, "");
		} catch (Exception e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, "系统异常");
		}
	}
	@Transactional(readOnly=false)
	public String introPrevious(DcSupExamQuotaInfo dcSupExamQuotaInfo) {
		// dao.setLastYearInfoList(dcSupExamQuotaInfo);
		//查询上一年报送单位指标info
		List<DcSupExamQuotaInfo> listInfo = dao.findExamQuotaInfoList(dcSupExamQuotaInfo);
		DcSupExamQuotaInfo dcSupExamQuotaInfo1 = new DcSupExamQuotaInfo();
		dcSupExamQuotaInfo1.setQuotaId(dcSupExamQuotaInfo.getQuotaId());
		List<DcSupExamQuotaInfo> list1 = dao.findList(dcSupExamQuotaInfo);
		for(DcSupExamQuotaInfo info :listInfo){
			for( DcSupExamQuotaInfo info1 :list1){
				if(StringUtils.equals(info.getQuotaContent(),info1.getQuotaContent())){
					throw new ServiceException("引入失败,目标内容："+info.getQuotaContent()+"已存在");
				}
			}
			DcSupExamQuotaOrg dcSupExamQuotaOrg = new DcSupExamQuotaOrg();
			dcSupExamQuotaOrg.setQuotaInfoId(info.getId());

			info.setId(null);
			info.setQuotaId(dcSupExamQuotaInfo.getQuotaId());
			info.setExamYear((Integer.parseInt(info.getExamYear())+1)+"");
			super.save(info);
			List<DcSupExamQuotaOrg> list = dcSupExamQuotaOrgService.findList(dcSupExamQuotaOrg);
			list.forEach(x->{
				x.setQuotaInfoId(info.getId());
				x.setQuotaId(dcSupExamQuotaInfo.getQuotaId());
				x.setId(null);
				dcSupExamQuotaOrgService.save(x);
			});
		}
		 return null;
	}
}